import axios from 'axios';

const APP_ID = 'wxf88a9112174021a1';
const APP_SECRET = '9c1c4c52fa226383ea796a5213540c39';

const SESSION_PATH = 'https://api.weixin.qq.com/sns/jscode2session';
const TOKEN_PATH = 'https://api.weixin.qq.com/cgi-bin/stable_token';
const USER_PHONE_PATH = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber';


export async function requestSessionToWx(code) {
  const result = await axios.get(SESSION_PATH, {
    params: {
      appid: APP_ID,
      secret: APP_SECRET,
      js_code: code,
      grant_type: 'authorization_code',
    },
  });
  const { openid } = result.data;
  return openid;
}


export async function getStableToken() {
  const result = await axios.post(TOKEN_PATH, {
    appid: APP_ID,
    secret: APP_SECRET,
    grant_type: 'client_credential',
    force_refresh: true,
  });
  const { access_token } = result.data;
  return access_token;
}


export async function getUserPhone(code, accessToken) {
  const result = await axios.post(`${USER_PHONE_PATH}?access_token=${accessToken}`, {
    code,
  });
  const { phone_info } = result.data;
  return phone_info;
}
